from aip import AipFace
import base64
import pprint
import os
import time
import pymysql
from photo import photos


def openphotos(filepath):
    image = (base64.b64encode(open(filepath, 'rb').read())).decode('utf-8')
    return image

APP_ID = '16145506'
API_KEY = 'pKC9aOrxSbgCC9H7ZPvaTaPf'
SECRET_KEY = 'HzqjknlD60b8OVLGy6RLkv4rB0iXVARQ'
client = AipFace(APP_ID, API_KEY, SECRET_KEY)

photos()
print('请稍等！')


yourlist=[]
yourdict={}
for filename in os.listdir(r"./photos"):
    image=openphotos('./photos/'+filename)
    groupId = "students"
    imageType = "BASE64"
    groupId = "students"
    result=client.search(image, imageType, groupId);
    # print(result)
    # print(result['result']['user_list'][0]['score'])
    if result['result']==None:
        print('你谁啊')
        continue
    else:
        name = result['result']['user_list'][0]['user_id']
        score = result['result']['user_list'][0]['score']
        yourlist.append(score)
        yourdict[str(score)] = str(name)
        # pprint.pprint(result)
    #time.sleep(1)
    break
yourlist = sorted(yourlist)
maxscore = yourlist[-1]
if maxscore > 80:
    yourid = yourdict[str(maxscore)]
    conn = pymysql.connect("47.110.150.123", "boss", "Gem1234@", "usersdata")
    cursor = conn.cursor()
    sql = """select username from data where userid='%s'""" % (yourid)
    cursor.execute(sql)
    name = cursor.fetchall()
    conn.commit()
    conn.close()
    print(name[0][0] + '签到成功!')
else:
    print('你变了-=-')